"""
Generate a report of a record. This is simply a cover sheet
which contains a EAN13 barcode to uniquely identify the
record, as well as a summary of the title,description,parties,
effective date, termination date,date of cover sheet generation,
number of attached documents, names of attached documents
"""
from tg import config
from reportlab.graphics.barcode import createBarcodeDrawing
from reportlab.platypus.paragraph import Paragraph
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus.frames import Frame
from reportlab.platypus import Table
from reportlab.lib.units import inch, cm
from reportlab.lib.pagesizes import A4,letter
from reportlab.lib import colors
import os

styles = getSampleStyleSheet()
styleN = styles['Normal']
styleH = styles['Heading1']

coversheet_storage = config.get('coversheets')

def genCoverSheet(identifier,pdfname,**kw):
    """
    identifier is the unique 13-digit EAN key
    pdfname is the file name of the pdf to be stored
    **kw contains as much information as possible about the
    record which is displayed on the cover sheet
    """
    identifier=str(identifier) # workaround as identifier gets sent in unicode for some reason
    output_pdf = os.path.join(coversheet_storage,pdfname)
    story = [] # keep track of the sections
    bcd = createBarcodeDrawing('EAN13', value='%s'%identifier) # unicode breaks this
    story.append(bcd)
    story.append(Paragraph("&nbsp;",styleH))
    if kw.has_key('title'):
        story.append(Paragraph("%s"%kw['title'],styleH))
        story.append(Paragraph("&nbsp;",styleH))
    if kw.has_key('description'):
        story.append(Paragraph("%s"%kw['description'],styleN))
    if kw.has_key('events'):
        if len(kw['events']):
            story.append(Paragraph("Events associated with this record",styleN))
            data = []
            data.append([Paragraph('Starts',styleN),Paragraph('Ends',styleN),Paragraph('Title',styleN),Paragraph('Description',styleN)])
            for x in kw['events']:
                data.append([Paragraph(x.fmtOccurs,styleN),Paragraph(x.fmtEnds,styleN),Paragraph(x.title,styleN),Paragraph(x.description,styleN)])
            # First the top row, with all the text centered and in Times-Bold,
            # and one line above, one line below.
            ts = [('ALIGN', (1,1), (-1,-1), 'LEFT'),
                ('LINEABOVE', (0,0), (-1,0), 1, colors.purple),
                ('LINEBELOW', (0,0), (-1,0), 1, colors.purple),
                ('FONT', (0,0), (-1,0), 'Times-Bold'),
                ('LINEABOVE', (0,-1), (-1,-1), 1, colors.purple),
                ('LINEBELOW', (0,-1), (-1,-1), 0.5, colors.purple,1, None, None, 4,1),
                ('LINEBELOW', (0,-1), (-1,-1), 1, colors.red),('FONT', (0,-1), (-1,-1), 'Times-Bold')]
            table = Table(data,)# style=ts)
            story.append(table)
    c=Canvas(output_pdf,pagesize=A4)
    f = Frame(cm, cm, 19*cm, 28*cm, showBoundary=0)
    f.addFromList(story, c)
    c.save()
    return output_pdf

